$( document ).ready(function() {
	// init calendar
//	var cal = $(".calendar").calendar({
//		otherDays: true,
//		 getDates: function(data){
//		},
//		click: function(d){
//			document.location = medi.contextPath + "/appointment/list?aptDate=" + d;
//		}
//	});
	
	// notify existance
//	if ($(".notify-window-container").length > 0) {
//		$.Dialog({
//			overlay: true,
//			shadow: true,
//			flat: true,
//			icon: '<span class="icon-info"></span>',
//			title: 'Notification',
//			content: '',
//			sysButtons: false,
//			onShow: function(_dialog){
//				var content = _dialog.children('.content');
//				content.html($(".notify-window-container").html());
//			}
//		});
//	}
	
	// validate appointment form
//	if ($(".registration-form").length > 0) {
//		$(".registration-form").validate({
//			
//			rules: {
//				"guiAppointment.date": {
//					required: true,
//					isAfterDate: true
//				}
//			},
//			
//			messages: {
//				"guiAppointment.date": {
//					required: "Please specify the appointment date",
//					isAfterDate : "Invalid Date. Please specify a valid date"
//				}
//			},
//			
//			submitHandler: function(form) {
//				form.submit();  
//			}
//		});
//	}

	// init delete button
	if ($(".apt-delete-btn").length > 0) {
		$(".apt-delete-btn").on('click', function(e){
			e.preventDefault();
			var deleteUrl = $(this).attr("href");
			$.Dialog({
				shadow: true,
				overlay: true,
				icon: '<span class="icon-remove"></span>',
				title: 'Delete Confirmation',
				width: 400,
				padding: 10,
				onShow: function(_dialog){
 					var content = _dialog.children('.content');
 					content.html('<div class="subheader-secondary" style="width:400px">Do you want to cancel this appointment?</div><br/><div><a id="btn-delete" href="' + deleteUrl + '" class="button large">Delete</a></div>');
				}
			});
		});
	}
	
	// init appointment date calendar
	$("#appointment-date-picker-input").on('change, blur', function() {
		var prevDate = $(this).val();
		setTimeout(function(){
			// there is some delay to set date from calendar close
			var selectedDate = $("#appointment-date-picker-input").val();
			if (isFutureDate(selectedDate)) {
				var sb = [];
				sb.push("?aptDate=" + selectedDate);
				if ($("#appointmentId").length > 0) {
					sb.push("&id=" + $("#appointmentId").val());
				}
				document.location = window.location.href.split('?')[0] + sb.join("");
			} else {
				$("#appointment-date-picker-input").val(prevDate);
				$.Dialog({
					shadow: true,
					overlay: true,
					icon: '<span class="icon-warning"></span>',
					title: 'Error Message',
					width: 400,
					padding: 10,
					onShow: function(_dialog){
						var content = _dialog.children('.content');
						content.html('<h4 class="fg-red">Invalid date. Please select a valid date for the appointment.</h4>');
					}
				});
			}
		}, 100);
	});
	
});

//jQuery.validator.addMethod("isAfterDate", function(value, element) {
//	var today = new Date();
//	today.setHours(0,0,0,0);
//	var selectDate = new Date(value);
//	selectDate.setHours(0,0,0,0);
//	if(today > selectDate) {
//        return false;
//    }
//	return true;
//}, "date should be equal or after today");

function isFutureDate(value) {
	var today = new Date();
	today.setHours(0,0,0,0);
	var selectDate = new Date(value);
	selectDate.setHours(0,0,0,0);
	if(today > selectDate) {
        return false;
    }
	return true;
}